unit cx_contacts;

interface
uses
 Windows,
 m_globaldefs, m_api, cx_vars;

procedure cx_UpdateStatus(hContact : integer; Status: Integer = ID_STATUS_ONLINE);
procedure cx_UpdateContact(hContact : integer; New: Boolean = true);
//function cx_AddRate(frID: Integer; frNick: String; StateIn: string; frSource: String; frCurrency: string; frAmount:integer): THandle;
procedure cx_RefreshContacts(IsOnline: Boolean = true);
//procedure cx_DeleteRate(zContact : integer);
function cx_FindRate(CurrFrom, CurrTo, CurrSource: String; CurrAmount: Integer): Integer;
//function cx_AddNewRate(CurrFrom, CurrTo, CurrSource: String): Integer;
{
function cx_ExtraListRebuild(hContact:WPARAM; lParam:LPARAM): int; cdecl;
function cx_ExtraImageApply(hContact:WPARAM; lParam:LPARAM): int; cdecl;
}
procedure UpdateCurrency(Data: ICurrency);

implementation

uses
  SysUtils,
  dbsettings,
  cx_procs;

{$include api/m_flags.inc}

{
procedure cx_DeleteRate(zContact : integer);
begin
 //pluginlink^.CallService(MS_PROTO_REMOVEFROMCONTACT,zContact, lParam(PChar(piShortName)));
 pluginlink^.CallService(MS_DB_CONTACT_DELETE, zContact, 0);
end;
}
procedure cx_RefreshContacts(IsOnline: Boolean = true);
var
  hContact : THandle;
begin
 hContact := pluginLink^.CallService(MS_DB_CONTACT_FINDFIRST, 0, 0);
 DecimalSeparator:='.';
  while hContact <> 0 do
  begin
    if IsContactProto(hContact, piShortName) = -1 Then
    begin
     if IsOnline then
       cx_UpdateContact(hContact)
     else
       cx_UpdateStatus(hContact, ID_STATUS_OFFLINE);
    end;
  hContact := pluginLink^.CallService(MS_DB_CONTACT_FINDNEXT, hContact, 0);
  end;
end;

procedure cx_UpdateStatus(hContact : integer; Status: Integer = ID_STATUS_ONLINE);
begin
 DBWriteContactSettingWord(hContact, piShortName, cx_ContactStatus, Status);
end;

procedure cx_UpdateContact(hContact : integer; New: Boolean = true);
var
 Nick, StatusMsg, NickFormat, StatusMsgFormat : string;
begin
 NickFormat:=DBReadString(hContact,piShortName,cx_opt_ContactCaption);
 if NickFormat='' then
   NickFormat:=DBReadString(0,piShortName,cx_opt_ContactCaption);
 StatusMsgFormat:=DBReadString(hContact,piShortName,cx_opt_StatusMessage);
 if StatusMsgFormat='' then
   StatusMsgFormat:=DBReadString(0,piShortName,cx_opt_StatusMessage);
 Nick:=cx_FormatString(NickFormat, hContact);
 StatusMsg:=cx_FormatString(StatusMsgFormat,hContact);

 DBWriteContactSettingString(hContact, piShortName, cx_Nick, PChar(Nick));
 DBWriteContactSettingString(hContact, 'CList', 'StatusMsg', PChar(StatusMsg));
 if New then
   cx_UpdateStatus(hContact)
 else
   cx_UpdateStatus(hContact, ID_STATUS_INVISIBLE);

 //TODO: Make additional settings on how the value should be displayed (just like in weather plugin)

end;


function cx_FindRate(CurrFrom, CurrTo, CurrSource: String; CurrAmount: Integer): Integer;
 var
  i: Integer;
begin
 Result:=-1;
 i:=0;
 while (i<=High(cx_Clist)) and (Result<0) do
  begin
   if (cx_Clist[i].Currency=CurrFrom) and (cx_Clist[i].StateIn=CurrTo) and (cx_Clist[i].Source=CurrSource) and (cx_Clist[i].Amount=CurrAmount) then
     Result:=i
   else
     Inc(i);
  end;
end;

procedure UpdateCurrency(Data: ICurrency);
begin
 //check delete mark
 if Data.Changes=cx_id_Delete then
  begin
   //delete contact
   if Data.hContact<>0 then
     pluginlink^.CallService(MS_DB_CONTACT_DELETE, Data.hContact, 0);
  end
 else
  begin
   //check anew contact
   if Data.hContact=0 then
    begin
     //new contact
     Data.hContact:=pluginLink^.CallService(MS_DB_CONTACT_ADD, 0, 0);
     if Data.hContact<>0 then
      begin
       pluginLink^.CallService(MS_PROTO_ADDTOCONTACT, Data.hContact, lParam(PChar(piShortName)));
       Data.Changes:=cx_id_All;
      end
     else
       Data.Changes:=0;
    end;
   //update data
   if (Data.Changes and cx_id_Name)>0 then
    try
     DBWriteContactSettingString(Data.hContact, piShortName, cx_Nick, PChar(Data.Name));
    except
     MessageBox(0, 'Nick write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   if (Data.Changes and cx_id_Amount)>0 then
    try
     DBWriteContactSettingDWord(Data.hContact, piShortName, cx_Amount, Data.Amount);
    except
     MessageBox(0, 'cx_Amount write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   if (Data.Changes and cx_id_Currency)>0 then
    try
     DBWriteContactSettingString(Data.hContact, piShortName, cx_Currency, PChar(Data.Currency));
    except
     MessageBox(0, 'cx_Currency write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   if (Data.Changes and cx_id_StateIn)>0 then
    try
     DBWriteContactSettingString(Data.hContact, piShortName, cx_StateIn, PChar(Data.StateIn));
    except
     MessageBox(0, 'cx_StateIn write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   if (Data.Changes and cx_id_Source)>0 then
    try
     DBWriteContactSettingString(Data.hContact, piShortName, cx_Source, PChar(Data.Source));
     UpdateCountry(Data.hContact, true);
    except
     MessageBox(0, 'cx_Source write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   if (Data.Changes and cx_id_Current)>0 then
    try
     DBWriteContactSettingString(Data.hContact, piShortName, cx_CurrentRate, PChar(Data.CurrentRate));
    except
     MessageBox(0, 'cx_CurrentRate write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   //0.3.0.6
   if (Data.Changes and cx_id_CC)>0 then
    try
     DBWriteContactSettingString(Data.hContact, piShortName, cx_opt_ContactCaption, PChar(Data.CC));
    except
     MessageBox(0, 'cx_ContactCaption write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   if (Data.Changes and cx_id_SM)>0 then
    try
     DBWriteContactSettingString(Data.hContact, piShortName, cx_opt_StatusMessage, PChar(Data.SM));
    except
     MessageBox(0, 'cx_StatusMessage write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
   //
   Data.Changes:=0;
  end;
end;
(*
function cx_AddRate(frID: Integer; frNick: String; StateIn:string; frSource: String; frCurrency: string; frAmount:integer): THandle;
var hContactNew: THandle; // handle to new contact
    //hContact: THandle;
    //DefaultGroup: WideString;
    zero  : PChar;
begin
  //Netlib_Log(cx_hNetlibUser, PChar('(cx_AddRate) Trying to add rate, id: '+IntToStr(frID)+', nick: '+frNick));
  zero:='0';
  // if really new contact
  hContactNew := pluginLink^.CallService(MS_DB_CONTACT_ADD, 0, 0);
  If hContactNew <> 0 Then
  Begin
//    DBWriteContactSettingDWord(hContactNew, piShortName, 'ID', frID);
    try
      DBWriteContactSettingString(hContactNew, piShortName, cx_Nick, PChar(frNick));
    except
        MessageBox(0, 'Nick write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
    try
//    DBWriteContactSettingWord(hContactNew, piShortName, 'Status', frStatus); // we can not update it here, it causes crash in newstatusnotify plugin
    DBWriteContactSettingString(hContactNew, piShortName, cx_StateIn, PChar(StateIn));                                                                              // so, the code is moved below
    except
        MessageBox(0, 'cx_StateIn write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
    try
        DBWriteContactSettingString(hContactNew, piShortName, cx_Source, PChar(frSource));
        UpdateCountry(hContactNew);
    except
        MessageBox(0, 'cx_Source write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
    try
            DBWriteContactSettingString(hContactNew, piShortName, cx_Currency, PChar(frCurrency));
    except
        MessageBox(0, 'cx_Currency write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
    try
            DBWriteContactSettingDWord(hContactNew, piShortName, cx_Amount, frAmount);
    except
        MessageBox(0, 'cx_Amount write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
    try
            DBWriteContactSettingString(hContactNew, piShortName, cx_CurrentRate, zero);
    except
        MessageBox(0, 'cx_CurrentRate write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
    try
            DBWriteContactSettingString(hContactNew, piShortName, cx_PreviousRate, zero);
    except
        MessageBox(0, 'cx_PreviousRate write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;
    try
            DBWriteContactSettingString(hContactNew, piShortName, cx_ComingRate, zero);
    except
        MessageBox(0, 'cx_ComingRate write failure...', piShortName, MB_OK or MB_ICONERROR);
    end;



    // assign group for contact, if given in settings
{    DefaultGroup := DBReadString(0, piShortName, opt_UserDefaultGroup, nil);
    if DefaultGroup<>'' then
      DBWriteContactSettingUnicode(hContactNew, 'CList', 'Group', PWideChar(DefaultGroup))
    else  }
  End;
  pluginLink^.CallService(MS_PROTO_ADDTOCONTACT, hContactNew, lParam(PChar(piShortName)));
  //pluginLink^.CallService(MS_CLIST_EXTRA_SET_ICON, hContactNew, lParam(PChar(piShortName)));

{  If hContactNew <> 0 Then
    DBWriteContactSettingWord(hContactNew, piShortName, 'Status', frStatus);}

 // Netlib_Log(cx_hNetlibUser, PChar('(vk_AddFriend) ... friend added'));
  Result := hContactNew;
end;
*)
{
function cx_ExtraListRebuild(hContact:WPARAM; lParam:LPARAM): int; cdecl;
 var
  i: Integer;
  CountryFrom, CountryTo: Integer;
  Icon: HICON;
begin
 if cx_IsFlags then
  begin
   for i:=0 to High(cx_Clist) do
    begin
     if cx_Clist[i].Image=0 then
      begin
       CountryFrom:=7;
       CountryTo:=375;
       Icon:=pluginLink^.CallService(MS_FLAGS_CREATEMERGEDFLAGICON, CountryFrom, CountryTo);
       MessageBox(0, PChar(IntToStr(Icon)), piShortName, MB_OK or MB_ICONINFORMATION);
       if Icon<>0 then
        begin
         cx_Clist[i].Image:=pluginLink^.CallService(MS_CLIST_EXTRA_ADD_ICON, Icon, 0);
         DestroyIcon(Icon);
        end;
       MessageBox(0, PChar(IntToStr(cx_Clist[i].Image)), piShortName, MB_OK or MB_ICONINFORMATION);
      end;
    end;
  end;
 //
 Result:=0;
end;

function cx_ExtraImageApply(hContact:WPARAM; lParam:LPARAM): int; cdecl;
 var
  i: Integer;
  iec: TIconExtraColumn;
begin
 if cx_IsFlags then
  begin
   for i:=0 to High(cx_Clist) do
    begin
     if hContact=cx_Clist[i].hContact then
      begin
       FillChar(iec, SizeOf(TIconExtraColumn), 0);
       iec.cbSize:=SizeOf(TIconExtraColumn);
       iec.ColumnType:=EXTRA_ICON_ADV1;
       iec.hImage:=cx_Clist[i].Image;
       //
       pluginLink^.CallService(MS_CLIST_EXTRA_SET_ICON, hContact, DWORD(@iec));
       //
       Break;
      end;
    end;
  end;
 //
 Result:=0;
end;
}

end.
